package cc.lj.internal.coupons.domain.martcap.model.vo.convert;

import cc.lj.internal.coupons.domain.martcap.model.vo.MartCapDO;
import cc.lj.internal.coupons.infrastructure.po.MartCapPO;
import org.mapstruct.MapMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;

import java.time.LocalDate;
import java.time.LocalDateTime;

@Mapper
public interface MartCapDOConvertPO {

    MartCapDOConvertPO INSTANCE = Mappers.getMapper(MartCapDOConvertPO.class);
    //do to po
    MartCapPO convert(MartCapDO martCapDO);

    /**
     * 这里需要特别说明一下，在转换的过程中，给DO增加了字段，这个字段希望自动填充
     * @param martCapPO
     * @return
     */
    @Mapping(source = "startDate",target = "isStart",qualifiedByName = "checkStartDate")
    @Mapping(source = "endDate",target = "isOver",qualifiedByName = "checkEndDate")
    MartCapDO convert(MartCapPO martCapPO);

    @Named("checkStartDate")
    static Integer checkStartDate(LocalDate startDate)
    {
        //liangxifeng 2022-01-02 等于当天也属于已开始
        LocalDate nowDate = LocalDate.now();
        if(startDate.isEqual(nowDate) || startDate.isBefore(nowDate)) {
            return 1;
        }
        return 0;
    }

    @Named("checkEndDate")
    static Integer checkEndDate(LocalDate endDate)
    {
        return endDate.isBefore(LocalDate.now())?1:0;
    }
}
